package com.higold.cloud.ui.adapter

import android.text.TextPaint
import android.text.style.ClickableSpan
import android.view.View
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.higold.cloud.R
import com.higold.cloud.mvp.model.bean.Data
import java.text.DecimalFormat



/**
 *    author : lxz
 *    date   : 2019/10/22 14:37
 *    desc   :
 */

class NewAgentAdapter(data: ArrayList<Data>) : BaseQuickAdapter<Data, BaseViewHolder>(R.layout.item_agent_list, data) {

    override fun convert(helper: BaseViewHolder, item: Data) {
        helper.setText(R.id.tv_agent_name, item.user_name)
                .setText(R.id.tv_agent_account, changeFloatToString(item.amount) + "W")
                .setText(R.id.tv_agent_rate, roundByScale(item.rate, 2) + "%")

        helper.getView<TextView>(R.id.tv_agent_name).isSelected = true
    }


    internal var clickableSpan: ClickableSpan = object : ClickableSpan() {
        override fun onClick(widget: View) {

        }

        override fun updateDrawState(ds: TextPaint) {

        }

    }

    private fun changeFloatToString(double: Double): String {
        val decimalFormat = DecimalFormat("##.##")
        return decimalFormat.format(double)
    }

    private fun getRate(double: Double): String {
        val decimalFormat = DecimalFormat("##.##")
        return decimalFormat.format(double)
    }

    /**
     * 将double格式化为指定小数位的String，不足小数位用0补全
     *
     * @param v     需要格式化的数字
     * @param scale 小数点后保留几位
     * @return
     */
    fun roundByScale(v: Double, scale: Int): String {
        require(scale >= 0) { "The   scale   must   be   a   positive   integer   or   zero" }
        if (scale == 0) {
            return DecimalFormat("0").format(v)
        }
        var formatStr = "0."
        for (i in 0 until scale) {
            formatStr += "0"
        }
        return DecimalFormat(formatStr).format(v)
    }


}
